package nongchang.kuaijiecaifu.com.ncapp_android.ui;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.NestedScrollView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.CoordUtil;
import com.amap.api.maps.AMapUtils;
import com.amap.api.maps.model.LatLng;


import butterknife.BindView;
import butterknife.ButterKnife;
import ezy.lite.util.UI;
import ezy.widget.adapter.TabsAdapter;
import ezy.widget.view.IconTextButton;
import kprogresshud.KProgressHUD;
import nongchang.kuaijiecaifu.com.ncapp_android.R;
import nongchang.kuaijiecaifu.com.ncapp_android.common.activity.BaseActivity;
import nongchang.kuaijiecaifu.com.ncapp_android.retrofit.data.HomeCartManager;
import nongchang.kuaijiecaifu.com.ncapp_android.retrofit.event.CartCountChange;
import nongchang.kuaijiecaifu.com.ncapp_android.retrofit.event.GoneTotalEvent;
import nongchang.kuaijiecaifu.com.ncapp_android.retrofit.event.VisibleTotalEvent;
import nongchang.kuaijiecaifu.com.ncapp_android.ui.fragment.FollowFragment;
import nongchang.kuaijiecaifu.com.ncapp_android.ui.fragment.MainCartFragment;
import nongchang.kuaijiecaifu.com.ncapp_android.ui.fragment.MainHomeFragment;
import nongchang.kuaijiecaifu.com.ncapp_android.ui.fragment.MainMyFragment;
import nongchang.kuaijiecaifu.com.ncapp_android.ui.fragment.VegetableBasketFragment;
import nongchang.kuaijiecaifu.com.ncapp_android.ui.widget.HomeViewPager;

import nongchang.kuaijiecaifu.com.ncapp_android.util.RxBus;
import nongchang.kuaijiecaifu.com.ncapp_android.util.ScaleUtil;

public class MainActivity extends BaseActivity {

    @BindView(R.id.pager)
    HomeViewPager mViewPager;
    @BindView(R.id.tabs)
    TabLayout mTabLayout;
    @BindView(R.id.txt_cart_num)
    TextView cartNum;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        setSwipeBackEnable(false);

        Fragment[] fragments = { new MainHomeFragment(), new VegetableBasketFragment(), new MainCartFragment(),new MainMyFragment()};
        mViewPager.setAdapter(new TabsAdapter(getSupportFragmentManager(), fragments));

        addTab(R.drawable.select_icon1, "首页");
        addTab(R.drawable.select_icon2, "菜篮子");
        addTab(R.drawable.select_icon3, "购物车");
        addTab(R.drawable.select_icon4, "我的");
        mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout));

        mTabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                mViewPager.setCurrentItem(tab.getPosition());
                if(tab.getPosition()!=2&&HomeCartManager.count()!=0){
                    cartNum.setVisibility(View.VISIBLE);
                }else{
                    cartNum.setVisibility(View.GONE);
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        RxBus.ofType(CartCountChange.class).compose(bindToLifecycle()).subscribe(event->{
            HomeCartManager.cartCount=event.count;
            if(event.count==0){
                cartNum.setVisibility(View.GONE);
            }else {
                cartNum.setVisibility(View.VISIBLE);
                cartNum.setText(event.count + "个\n￥" + HomeCartManager.getTotal());
            }
        });
    }
    private void addTab(int icon, String text) {
        IconTextButton view = (IconTextButton) getLayoutInflater().inflate(R.layout.widget_tab_item, mTabLayout, false);
        view.setmIconDrawable(getResources().getDrawable(icon));
        view.setText(text);
        mTabLayout.addTab(mTabLayout.newTab().setCustomView(view));
    }
    @Override
    public void onBackPressed() {
        if(UI.isFastClick()){
            super.onBackPressed();
            this.finish();
            //            System.exit(0);
            return;
        }
        UI.showToast(this, "再按一次退出程序！");
    }

}
